﻿package _3_Panel;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
 * 2011-6-7 1:23:26
 * @author Administrator
 * 该程序演示的是在JTextArea的上方动态的显示一个面板，面板中有一个按钮
 * 先输入一些文件，在用鼠标选中一部分，当选中后释放鼠标左键时，就显示出来了，
 * 再次点击左键，就消失了
 */
public class TJLayeredPane {
    JFrame frame;
    Container contentPane;
    JLayeredPane layeredPane;
    JTextArea textArea;
    JPanel operatorPane;
    public TJLayeredPane(){
        frame=new JFrame("重叠面板测试");
        contentPane=frame.getContentPane();
        layeredPane=new JLayeredPane();
        contentPane.add(layeredPane);
        textArea=new JTextArea(10,40);
        textArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                layeredPane.remove(operatorPane);
                layeredPane.repaint();
                super.mousePressed(e);
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                if(textArea.getSelectedText()!=null){
                    layeredPane.add(operatorPane,new Integer(20));
                }
                super.mouseReleased(e);
            }
        });
        JScrollPane jsp=new JScrollPane(textArea);
        layeredPane.add(jsp,new Integer(10));
        layeredPane.setPreferredSize(jsp.getPreferredSize());
        Dimension d=jsp.getPreferredSize();
        jsp.setBounds(0, 0, (int)d.getWidth(), (int)d.getHeight());
        operatorPane=new JPanel();
        System.out.println(operatorPane.getLayout());
        JButton fontButton=new JButton("字体");
        fontButton.setFocusable(false);
        operatorPane.add(fontButton);
        operatorPane.setSize(new Dimension(100,100));
        operatorPane.setLocation(20, 20);
        DragAdapter adapter=new DragAdapter();
        operatorPane.addMouseListener(adapter);
        operatorPane.addMouseMotionListener(adapter);
//        layeredPane.add(operatorPane,new Integer(20));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String args[]){
        new TJLayeredPane();
    }
    class DragAdapter implements MouseListener,MouseMotionListener{
        private Point p1;
        private Point p2;
        private Point position;
        public DragAdapter(){
            p1=null;
            p2=null;
        }
        public void mouseDragged(MouseEvent e) {
            p2=e.getPoint();
            position=operatorPane.getLocation();
            int x=position.x+p2.x-p1.x;
            int y=position.y+p2.y-p1.y;
            operatorPane.setLocation(x,y);
        }
        public void mousePressed(MouseEvent e) {
            p1=e.getPoint();
        }
        public void mouseExited(MouseEvent e) {

        }

        public void mouseEntered(MouseEvent e) {

        }

        public void mouseMoved(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}

    }
}
